package com.zch.news.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


/**
 * 
 * @ClassName: DBConnection 
 * @Description: 获得数据库连接
 * @author: zch
 * @version: 1.10
 * @date: 2018年8月28日 下午2:28:12
 */
public class DBConnection {
	private static Connection conn = null;
	public static Connection getConnection() {
		try {
			//加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			//获得数据库连接对象
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zch", "张诚华", "060010");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	//测试是否获得数据库成功
	public static void main(String[] args) {
		Connection conn = DBConnection.getConnection();
		if(conn == null) {
			System.out.println("数据库连接失败！");
		}else {
			System.out.println("数据库连接成功！");
		}
	}
}
